package com.xpec.c4.game.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpec.c4.game.character.CharacterSender;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.contexts.Contexts;
import com.xpec.c4.service.contexts.ContextsListener;
import com.xpec.c4.service.contexts.ScopeType;
import com.xpec.c4.service.contexts.SessionContext;
import com.xpec.c4.service.ioc.IocContainer;
import com.xpec.c4.service.server.CloseType;
import com.xpec.c4.service.utility.Utility;

/**
 * 
 * 
 * @author lipeilin
 * @2011-6-20 下午07:54:51
 * 
 */
public class ContextsStatusListener implements ContextsListener {

	private static final Log log = LogFactory
			.getLog(ContextsStatusListener.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.xpec.c4.service.contexts.ContextsListener#endSessionContexts(java
	 * .lang.String)
	 */

	@Override
	public void endSessionContexts(GUID sessionID, CloseType type) {
		try {
			log.info("session contexts end:" + sessionID);
			Contexts.putSessionIDInEventContext(sessionID);
			Contexts.flushSessionIDUserID();

			if (sessionID != null) {
				Contexts.putInContexts("sessionID", sessionID, ScopeType.EVENT);
			}

			CharacterSender action = IocContainer
					.createObject(CharacterSender.class);

			GUID playerID = action.playerID;
			// if( playerID == null )
			// return;
			
			action.playerLogOut(type);

//			if (playerID != null)
//				Contexts.endUserContextMessage(playerID);

			// Contexts.destroySessionContext();
			// Contexts.destroyUserContext();
		} catch (Exception e) {
			log.error("endSessionContexts error:" + sessionID, e);
		}
		finally{
			// 清除event context
			Contexts.destroyEventContext();
		}
	}

	// TODO
	// FIXME
	// TODO
	// FIXME
	// TODO
	// FIXME
	// TODO
	// FIXME
	// TODO
	// FIXME

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.xpec.c4.service.contexts.ContextsListener#endSessionContexts(java
	 * .lang.String)
	 */
	/*
	 * @Override public void endSessionContexts(String sessionID) {
	 * 
	 * // 现在某条session 掉线, 没办法知道有哪些模块与这个session有关, // 所以广播信息给所有模块？
	 * 
	 * 
	 * // 这边流程要改
	 * 
	 * 
	 * // 先简单解决离线的问题
	 * 
	 * // log.info("session contexts end:" + sessionID);
	 * 
	 * // List<BasicServiceModule> al =
	 * ModuleServer.getInstance().getAllModules();
	 * 
	 * // for(BasicServiceModule it:al){ // it.addMessage(messageType,
	 * messageContentBytes, sessionID, srcService, options) // }
	 * 
	 * 
	 * String userID = Contexts.getUserIDBySessionID(sessionID);
	 * 
	 * if(userID != null){ UserContext uc =
	 * Contexts.getUserContextsByUserID(userID);
	 * 
	 * if(uc != null){
	 * 
	 * Object p = uc.get("player");
	 * 
	 * // 如果有player, 就交给player去处理离线的问题
	 * 
	 * if(p != null){
	 * 
	 * Player player = (Player)p;
	 * 
	 * // player.getZoneId();
	 * 
	 * // TODO // FIXME
	 * 
	 * // 这边要发离线消息给zone module // 待订离线的消息 GroupMessage message = new
	 * GroupMessage();
	 * 
	 * // CharacterSender action =
	 * IocContainer.createObject(CharacterSender.class); // String playerID =
	 * action.playerID; // action.playerLogOut();
	 * 
	 * // 逻辑参考 // CharacterSender.playerLogOut();
	 * 
	 * MessageAction.sendMessage(message);
	 * 
	 * 
	 * } else{
	 * 
	 * // 如果没有player, 则修改account就好 SessionContext sc =
	 * Contexts.getSessionContextsBySessionID(sessionID);
	 * 
	 * if(sc != null){ Object a = sc.get("account");
	 * 
	 * if(a != null){ Account account = (Account) a; account.offline();
	 * account.setSessionID(null); } }
	 * 
	 * 
	 * 
	 * }
	 * 
	 * }
	 * 
	 * 
	 * }
	 * 
	 * }
	 */

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.xpec.c4.service.contexts.ContextsListener#endUserContexts(java.lang
	 * .String, java.lang.String)
	 */
	@Override
	public void endUserContexts(String userID, GUID sessionID) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.xpec.c4.service.contexts.ContextsListener#startSessionContexts(java
	 * .lang.String)
	 */
	@Override
	public void startSessionContexts(GUID sessionID) {
		String ip = Utility.getIpBySessionID(sessionID);
		SessionContext context = Contexts
				.getSessionContextsBySessionID(sessionID);
		if (context != null) {
			context.set("clientIP", ip);
		} else {
			log.error("context is null! sessionID:" + sessionID + " clientIP:"
					+ ip);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.xpec.c4.service.contexts.ContextsListener#startUserContexts(java.
	 * lang.String, java.lang.String)
	 */
	@Override
	public void startUserContexts(String userID, GUID sessionID) {

	}

}
